package cc.wfu.domain.cart.service;

import cc.wfu.domain.cart.model.entity.CartEntity;
import cc.wfu.domain.cart.repository.ICartRepository;
import cc.wfu.types.common.Constants;
import cc.wfu.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 购物车服务
 * @Date: 2024-06-13-15:54
 */
@Slf4j
@Service
public class CartService {

    @Resource
    protected ICartRepository repository;

    /**
     * 新增购物车
     * @param cartEntity 购物车实体
     */
    public void addShopCart(CartEntity cartEntity) {
        if (repository.addShopCart(cartEntity) != 1) {
            throw new AppException("添加购物车失败！");
        }
    }


    /**
     * 查询购物车
     */
    public List<CartEntity> queryShopCart(String userId) {
        return repository.queryShopCart(userId);
    }

    /**
     * 删除购物车
     */
    public void deleteCart(String userId, String cartId) {
        if (repository.deleteCart(userId, cartId) != 1) {
            throw new AppException(Constants.ResponseCode.DELETE_CART_ERROR.getInfo(), Constants.ResponseCode.DELETE_CART_ERROR.getCode());
        }
    }
}
